<template>
  <el-form :model="form" label-width="auto">
    <el-form-item label="产品名称">
      <el-input v-model="form.title" />
    </el-form-item>
    <el-form-item label="选择分类">
      <el-select v-model="form.classid" placeholder="请选择分类">
        <el-option
          :label="item.classname"
          :value="item.id"
          v-for="(item, index) in data.catList"
          :key="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="产品单价">
      <el-input v-model="form.sprice" />
    </el-form-item>
    <el-form-item label="产品价格">
      <el-input v-model="form.cprice" />
    </el-form-item>
    <el-form-item label="产品简介">
      <el-input v-model="form.info" />
    </el-form-item>
    <!-- <el-form-item label="排序">
      <el-input v-model="form.px" />
    </el-form-item> -->
    <el-form-item label="产品图片">
      <el-upload
        class="avatar-uploader"
        action="https://pro.z550.cn/upload/saveFile"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
        :headers="getHeaders()"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { getCategoryList, addProduct } from "@/api/index";
import router from "@/route/index";
import { useRoute } from "vue-router";
const route = useRoute();
console.log(route.params.id);
const form = reactive({
  classname: "",
  classid: 0,
  sprice: null,
  cprice: null,
  info: "",
  picture: "",
});
const data = reactive({});
const onSubmit = async () => {
  if (!form.title) {
    ElMessage.error("请输入产品名称");
    return;
  }
  if (form.classid == 0) {
    ElMessage.error("请选择分类");
    return;
  }
  if (!form.sprice) {
    ElMessage.error("请输入产品单价");
    return;
  }
  addProduct(form).then((res) => {
    if (res.code == 200) {
      ElMessage.success("产品添加成功！");
      setTimeout(() => {
        router.push({ name: "productList" });
      }, 500);
    }
  });
};
const getHeaders = () => {
  const token = sessionStorage.getItem("token");
  let headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  return headers;
};
const getCateList = () => {
  getCategoryList().then((res) => {
    console.log(res);
    if (res.code == 200) {
      res.data.unshift({
        id: 0,
        classname: "请选择分类",
      });
      data.catList = res.data;
    }
  });
};
const imageUrl = ref("");

const handleAvatarSuccess = (res, uploadFile) => {
  if (res.code == 200) {
    imageUrl.value = URL.createObjectURL(uploadFile.raw);
    form.picture = res.data.url;
  } else {
    ElMessage.error("上传失败" + res.msg);
  }
};

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("图片大小不能超 2MB!");
    return false;
  }
  return true;
};
onMounted(() => {
  getCateList();
});
</script>

<style lang="less" scoped>
.avatar-uploader {
  border: 1px dashed var(--bs-theme-color);
  border-radius: 6px;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>